// mypage/collCoup/collCoup.js
var app=getApp();
var util=require("../../utils/util");
var my=require("../../api/my");
var home=require("../../api/home");


Page({
  /*页面的初始数据*/
  data: {
    nav_list: ["附近","兴趣教育","学科辅导"],
    pref_text: ['立减券','满减券','礼品券','','平台券'],
    cur_nav: 0,
    pref_list: [],
    currentPage: 1,
    pageSize: 10,
    hasMoreDate: true,
    lat: 22.80536,
    lng: 113.29321,
    token: '',
    show_nav: false
  },
  /*生命周期函数--监听页面加载*/
  onLoad: function (options) {
    var that=this;
    wx.getStorage({
      key: 'city',
      success:function(res){
        that.setData({lat:res.data.lat,lng:res.data.lng})
        //
        that.get_every_pref_list(0)
      },
    })
    that.setData({token:wx.getStorageSync('token')})
  },
  // 优惠券切换
  chage_nav:function(e){
    var that=this,cur_nav=that.data.cur_nav;
    var index=e.currentTarget.dataset.index;
    if(cur_nav != index){
      that.setData({cur_nav:index,pref_list:[],currentPage:1,show_nav:false})
      //
      that.get_every_pref_list(index)
    }
  },
  // 适用范围
  open_more:function(e){
    var that=this,pref_list=that.data.pref_list;
    var index=e.currentTarget.dataset.index;
    if(pref_list[index].couponWay==4){
      var businessNames=pref_list[index].businessNames?pref_list[index].businessNames:'';
      wx.showToast({
        title: pref_list[index].couponUsingRange==1?'适用机构：'+businessNames:"适用平台全部机构",
        icon: 'none',
        duration: 3000
      })
    }else{
      var businessName=pref_list[index].businessName?pref_list[index].businessName:'';
      var courseNames=pref_list[index].courseNames?pref_list[index].courseNames:'';
      wx.showToast({
        title: pref_list[index].couponUsingRange==1?'适用'+businessName+'以下课程：'+courseNames:'适用'+businessName+'全部课程',
        icon: 'none',
        duration: 3000
      })
    }
  },
  nav_back:function(){
    wx.navigateBack({
      delta: 1
    });
  },
  nav_mech:function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../../pages/mechDetail/mechDetail?id='+id+'&status=9',
    })
  },
  // 领取优惠券
  ling_pref:function(e){
    var that=this,pref_list=that.data.pref_list,token=that.data.token,userinfo=wx.getStorageSync('userinfo');
    var index=e.currentTarget.dataset.index;
    var params={
      "couponId": pref_list[index].id
    }
    //
    that.user_get_pref(params,index,token)
    this.setData({pref_list: pref_list})
  },
  // 触底
  onReachBottom:function(){
    var that=this,hasMoreData=that.data.hasMoreData,index=that.data.cur_nav;
    if(hasMoreData){
      that.get_every_pref_list(index)
    }else{
      util.page_show_toast('没有更多了',1500)
    }
  },
  // 获取优惠券列表
  get_every_pref_list:function(index){
    wx.showLoading({title: '加载中'})
    var that=this,currentPage=that.data.currentPage,pageSize=that.data.pageSize,token=that.data.token,pref_text=that.data.pref_text;
    var params={
      "currentPage": currentPage,
      "pageSize": pageSize,
      "type": index,
      "lat": that.data.lat,
      "lng": that.data.lng
    }
    my.get_every_pref_list(params,token).then(res=>{
      if(res.data.code==200){
        var list=res.data.data.records;
        for(var i=0;i<list.length;i++){
          list[i].couponStartTime=util.formatTime_year(new Date(list[i].couponStartTime))
          list[i].couponEndTime=util.formatTime_year(new Date(list[i].couponEndTime))
          list[i].space=list[i].distance>1000?(list[i].distance/1000).toFixed(2):list[i].distance;
          list[i].couponWayName=pref_text[list[i].couponWay]
        }
        var pref_list = that.data.pref_list;
        pref_list = util.extend(pref_list, list)
        if (list.length < pageSize) {
          that.setData({
            pref_list: pref_list,
            hasMoreData: false
          })
        } else {
          that.setData({
            pref_list: pref_list,
            hasMoreData: true,
            currentPage: currentPage + 1
          })
        }
        if(list.length==0&&pref_list.length==0){
          that.setData({show_nav:true})
        }
      }
    })
  },
  // 领取优惠券
  user_get_pref:function(params,index,token){
    var that=this,pref_list=that.data.pref_list;
    home.get_pref(params,token).then(res=>{
      if(res.data.code==200){
        util.page_show_toast('领取成功',1500)
        pref_list[index].isReceive=1;
        that.setData({pref_list:pref_list})
      }
    })
  },
})